﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TasksManagerService.cs" company="JaguarLabs">
//   JaguarLabs 2013
// </copyright>
// <summary>
//   Used to provide the service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TasksManagerServices
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using TasksManagerServices.Providers;
    using TasksManagerServices.Value_Objects.Services;
    using TasksManagerServices.ValueObjects;

    public class TasksManagerService : ITasksManagerService
    {
        /// <summary>
        /// Gets the user tasks.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public IEnumerable<Task> GetUserTasks(string user)
        {
            return TaskProvider.GetUserTasks(user);
        }

        /// <summary>
        /// Inserts the task.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="priorityId">The priority id.</param>
        /// <param name="notificationDate">The notification date.</param>
        /// <param name="projectId">The project id.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="showInCalendar">if set to <c>true</c> [show in calendar].</param>
        /// <param name="users">The users.</param>
        /// <returns></returns>
        public InsertResponse InsertTask(string name, string description, DateTime endDate, int priorityId,
                              DateTime? notificationDate, int projectId, int progress, bool showInCalendar,
                              string[] users)
        {
            try
            {
                var newTaskId = TaskProvider.InsertTask(new Task
                    {
                        Name = name,
                        Description = description,
                        EndDate = endDate,
                        PriorityId = priorityId,
                        NotificationDate = notificationDate,
                        ProjectId = projectId,
                        ShowInCalendar = showInCalendar,
                        Progress = progress,
                        Users = users.Any() ? users.Select(user => user).ToList() : null,
                        Attachments = new List<Attachment>()
                    });

                return new InsertResponse
                    {
                        TaskId = newTaskId,
                        ServiceResponseStatusType = ServiceResponseStatusType.Success
                    };
            }
            catch (Exception ex)
            {
                return new InsertResponse
                    {
                        ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                        ErrorMessage = ex.Message
                    };
            }
        }

        /// <summary>
        /// Updates the task.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="priorityId">The priority id.</param>
        /// <param name="notificationDate">The notification date.</param>
        /// <param name="projectId">The project id.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="showInCalendar">if set to <c>true</c> [show in calendar].</param>
        /// <param name="users">The users.</param>
        /// <returns></returns>
        public UpdateResponse UpdateTask(int id, string name, string description, DateTime endDate, int priorityId,
                              DateTime? notificationDate, int projectId, int progress, bool showInCalendar,
                              string[] users)
        {
            try
            {
                var updated = TaskProvider.UpdateTask(new Task
                {
                    Id = id,
                    Name = name,
                    Description = description,
                    EndDate = endDate,
                    PriorityId = priorityId,
                    NotificationDate = notificationDate,
                    ProjectId = projectId,
                    ShowInCalendar = showInCalendar,
                    Progress = progress,
                    Users = users.Any() ? users.Select(user => user).ToList() : null,
                    Attachments = new List<Attachment>()
                });

                return new UpdateResponse
                {
                    Updated = updated,
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
            }
            catch (Exception ex)
            {
                return new UpdateResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = ex.Message
                };
            }
        }

        /// <summary>
        /// Deletes the task.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public UpdateResponse DeleteTask(int id)
        {
            try
            {
                var updated = TaskProvider.DeleteTask(id);
                return new UpdateResponse
                {
                    Updated = updated,
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
            }
            catch (Exception ex)
            {
                return new UpdateResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = ex.Message
                };
            }
        }

        /// <summary>
        /// Gets the projects.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Project> GetProjects()
        {
            return TaskProvider.GetProjects();
        }
    }
}